package com.zhuhjay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.common.exception.CustomException;
import com.zhuhjay.system.domain.entity.LoginLog;
import com.zhuhjay.system.mapper.LoginLogMapper;
import com.zhuhjay.system.service.ILoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuhjay
 * @since 2022-03-20
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Override
    public Page<LoginLog> getLoginLog(Integer pageNum, Integer pageSize) {
        Page<LoginLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<LoginLog> getLoginLogByStatus(Integer pageNum, Integer pageSize, Integer status) {
        Page<LoginLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status).orderByDesc("id");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void deleteLoginLog(List<Integer> ids) {
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .in("id", ids);
        int delete = baseMapper.delete(queryWrapper);
        if( delete < 1 ){
            throw new CustomException("登录日志删除失败");
        }
    }

}
